package org.liveboardgames.agricola.domain.board;

import java.util.HashMap;
import java.util.Map;

import org.liveboardgames.agricola.domain.card.ActionCard;
import org.liveboardgames.agricola.domain.common.IResourceContainer;
import org.liveboardgames.agricola.domain.common.Target;
import org.liveboardgames.agricola.model.enums.BoardTileTypeEnum;
import org.liveboardgames.agricola.model.structure.Visitable;
import org.liveboardgames.agricola.model.structure.Visitor;

public class BoardTile implements Target, Visitable, IResourceContainer {

  private Integer uniqueId;

  private Integer id;

  private ActionCard actionCard;

  private Map<String, Integer> resources;

  private BoardTileTypeEnum type;

  public void setUniqueId(Integer uniqueId) {
    this.uniqueId = uniqueId;
  }

  public Integer getUniqueId() {
    return uniqueId;
  }

  /**
   * @return the {@link #id}
   */
  public Integer getId() {
    return id;
  }

  /**
   * @param id
   *          the {@link #id} to set
   */
  public void setId(Integer id) {
    this.id = id;
  }

  /**
   * @return the {@link #actionCard}
   */
  public ActionCard getActionCard() {
    return actionCard;
  }

  /**
   * @param actionCard
   *          the {@link #actionCard} to set
   */
  public void setActionCard(ActionCard actionCard) {
    this.actionCard = actionCard;
  }

  /**
   * @return the {@link #resources}
   */
  public Map<String, Integer> getResources() {
    return resources;
  }

  /**
   * @param resources
   *          the {@link #resources} to set
   */
  public void setResources(Map<String, Integer> resources) {
    this.resources = resources;
  }

  /**
   * @return the {@link #type}
   */
  public BoardTileTypeEnum getType() {
    return type;
  }

  /**
   * @param type
   *          the {@link #type} to set
   */
  public void setType(BoardTileTypeEnum type) {
    this.type = type;
  }

  public void addResource(String type, Integer quantity) {
    if (this.resources == null) {
      this.resources = new HashMap<String, Integer>();
    }
    Integer currentQuantity = this.resources.get(type);
    if (currentQuantity == null) {
      currentQuantity = Integer.valueOf(0);
    }
    this.resources.put(type, Math.max(0, currentQuantity + quantity));
  }

  public void addResources(Map<String, Integer> resources) {
    if (resources != null) {
      for (String type : resources.keySet()) {
        addResource(type, resources.get(type));
      }
    }
  }

  public void acceptVisit(Visitor visitor) {
    visitor.visit(this);

    if (this.actionCard != null) {
      this.actionCard.acceptVisit(visitor);
    }
  }
}
